package business;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author wangtengyu
 * @Create 2018-06-06-8:58
 */
public class Test {

    public static void main(String[] args) {
        Trader raou1 = new Trader("Raou1", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raou1, 2012, 1000),
                new Transaction(raou1, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        Map<Trader,List<Transaction>> map=transactions.stream().collect(Collectors.groupingBy(Transaction::getTrader));

        //(1)找出2011年发生的交易，并按交易额排序
/*        List<Transaction> list = transactions.stream().filter(a -> a.getYear() == 2012).sorted(Comparator.comparing(Transaction::getValue).reversed()).collect(Collectors.toList());
        list.forEach(System.out::println);*/

        //(2)交易员在哪些不同的城市生活过 set的方式可以去掉distinct
/*        List<String> citis= transactions.stream().map(transaction -> transaction.getTrader().getCity()).distinct().collect(Collectors.toList());
        Set<String> citySet= transactions.stream().map(transaction -> transaction.getTrader().getCity()).collect(Collectors.toSet());
        citis.forEach(System.out::println);
        citySet.forEach(System.out::println);*/

        //(3)查找所有来自剑桥的交易员，并按姓名排序
/*        List<Trader> traderList = transactions.stream()
                .map(Transaction::getTrader)
                .distinct()
                .filter(trader -> trader.getCity() == "Cambridge")
                .sorted(Comparator.comparing(Trader::getName).reversed())//如果这里面试lambada就没法操作了
                .collect(Collectors.toList());
        traderList.forEach(System.out::println);*/

        //(4)返回所有交易员的姓名字符串，按字母顺序排序
        /*String nameString= transactions.stream()
                .map(Transaction::getTrader)
                .map(Trader::getName)
                .distinct()
                .sorted()
//                .sorted(Comparator.comparing(a->a.toString()).reversed())
                .reduce("",(a,b)->a+b);
        System.out.println(nameString);*/

        //(5)有没有交易员是在米兰工作的  要用negate方法需要实例化predicate对象
        /*Predicate predicate=new Predicate() {
            @Override
            public boolean test(Object o) {
                return o.equals("Milan");
            }
        };


        Boolean flag=transactions.stream()
                .map(transaction -> transaction.getTrader().getCity())
                .anyMatch(city->city=="Milan");
        System.out.println(flag);*/

        //(6)打印所有在剑桥的交易员的交易额
       /* Integer sum=transactions.stream()
                .filter(transaction -> transaction.getTrader().getCity()=="Cambridge")
                .map(Transaction::getValue)
                .reduce(0,Integer::sum);
        System.out.println(sum);*/
        //forEach 是没有返回值的
        /*transactions.stream()
                .filter(transaction -> transaction.getTrader().getCity()=="Cambridge")
                .map(Transaction::getValue)
                .forEach(System.out::println);*/


        //(7)所有交易中最高的交易额是
        /*Optional<Integer> max=transactions.stream()
                .map(Transaction::getValue)
                .reduce(Integer::max);
        System.out.println(max.get());*/


        //(8)找到交易额最小的交易
       /* Optional<Transaction> optional=transactions.stream()
                .sorted(Comparator.comparing(Transaction::getValue))
                .findFirst();
        System.out.println(optional.get().getValue());

        Optional<Transaction> optional1= transactions.stream()
                .min(Comparator.comparing(Transaction::getValue));
        System.out.println(optional1.get().getValue());

        Optional<Transaction> optional2=transactions.stream()
                .reduce((a,b)->a.getValue()<b.getValue()?a:b);
        System.out.println(optional2.get().getValue());*/


    }


}
